package com.ledim.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v4.view.PagerAdapter;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.RelativeLayout;
import com.facebook.drawee.view.SimpleDraweeView;
import com.ledim.adapter.base.LedimBaseListAdapter;
import com.ledim.bean.ExpressionPackageBean;
import com.ledim.utils.FrescoUtil;
import com.ledim.widget.view.NoScrollGridView;
import com.letv.android.young.client.R;
import java.util.List;

/**
 * 表情集adapter
 */
public class EmojiPageSetAdapter extends PagerAdapter {

    private List<List<ExpressionPackageBean>> mPageEmojiList;
    private Context mContext;
    private OnClickDimIconListner onClickDimIconListner;
    private boolean mIsFullScreen;

    public EmojiPageSetAdapter(Context context, List<List<ExpressionPackageBean>> list,
                               OnClickDimIconListner onClickDimIconListner, boolean fullScreen) {
        super();
        this.mContext = context;
        this.mPageEmojiList = list;
        this.onClickDimIconListner = onClickDimIconListner;
        this.mIsFullScreen = fullScreen;
    }

    @Override
    public int getCount() {
        return mPageEmojiList.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        NoScrollGridView view = null;
        //if(position != 0){
        view = new NoScrollGridView(mContext);
        final EmojiAdapter adapter = new EmojiAdapter(mContext, mPageEmojiList.get(position));
        view.setAdapter(adapter);
        view.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (onClickDimIconListner != null) {
                    onClickDimIconListner.onClickImage(adapter.getItem(position));
                }
            }
        });
        if (mIsFullScreen) {
            view.setNumColumns(7);
        } else {
            view.setNumColumns(4);
        }

        view.setBackgroundColor(Color.TRANSPARENT);
        view.setHorizontalSpacing(1);
        view.setVerticalSpacing(1);
        view.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
        view.setCacheColorHint(0);
        view.setPadding(1, 0, 1, 0);
        view.setSelector(new ColorDrawable(Color.TRANSPARENT));
        view.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        view.setGravity(Gravity.CENTER);
        RelativeLayout.LayoutParams layoutParams =
                (RelativeLayout.LayoutParams) view.getLayoutParams();
        layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
        view.setVerticalScrollBarEnabled(false);
        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    /**
     * 表情填充器
     */
    static class EmojiAdapter extends LedimBaseListAdapter<ExpressionPackageBean> {
        public EmojiAdapter(Context context, List<ExpressionPackageBean> list) {
            super(context);
            setDatas(list);
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder;
            if (convertView == null) {
                viewHolder = new ViewHolder();
                convertView = mInflater.inflate(R.layout.emoji_item, null, false);
                viewHolder.emoji = (SimpleDraweeView) convertView.findViewById(R.id.mp_emoji_icon);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            FrescoUtil.loadUrl(getItem(position).url, viewHolder.emoji);
            return convertView;
        }

        static class ViewHolder {

            public SimpleDraweeView emoji;
        }
    }

    public interface OnClickDimIconListner {

        void onClickImage(ExpressionPackageBean image);
    }
}